package com.example.springboot.config;

import com.alibaba.fastjson.JSON;
import javafx.geometry.Point2D;

public class GeoUtils {

    private static Point2D orignPoit = new Point2D(-180, 90);

    public static void main(String[] args) {
        Point2D p = new Point2D(119.2, 32);
        System.out.println(JSON.toJSONString(caculateByScale(p, 1/2000000)));
        System.out.println(JSON.toJSONString(caculateByLevel(p, 21)));
    }

    public static long[] caculateByScale(Point2D point, double levelScale) {
        double plottingResolution = getPlottingResolutionByScale(levelScale);
        return caculateColAndRowByPlottingResolution(point, plottingResolution);
    }
    public static long[] caculateByLevel(Point2D point, int levelScale) {
        double plottingResolution = getPlottingResolutionByLevel(levelScale);
        return caculateColAndRowByPlottingResolution(point, plottingResolution);
    }

    public static long[] caculateColAndRowByPlottingResolution(Point2D point, double plottingResolution) {
        long col1 = (int) Math.floor((point.getX() -  orignPoit.getX()) / 256 / plottingResolution);
        long row1 = (int) Math.floor((Math.abs(point.getY() -  orignPoit.getY())) / 256 / plottingResolution);
        return new long[]{row1, col1};
    }

    public static double getPlottingResolutionByScale(double levelScale) {
        // Scale = 1 : (96 * 2 * Math.PI * 6378137 * resolution / 360 / 0.0254)
        return levelScale * 360 / (96 * 2 * Math.PI * 6378137 * 0.0254);
    }

    public static double getPlottingResolutionByLevel(int levelScale) {
        // Scale = 1 : (96 * 2 * Math.PI * 6378137 * resolution / 360 / 0.0254)
//   假设：1.使用经纬度；2.切片大小：256像素×256像素     第1级地图(L0)的分辨率 = （360度）/(256) ；第2级(L1)分辨率=260/256/2
        return 260/256/Math.pow(2,levelScale-1);
    }



}
